package com.leha.forum.lehare.controller;

import com.leha.forum.lehare.entity.Category;
import com.leha.forum.lehare.response.Result;
import com.leha.forum.lehare.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lovelycat
 * @version 1.0
 * @since 2025-04-15 22:26
 */
@RestController
@RequestMapping("/category")
public class PostCategoryController {
    @Resource
    private CategoryService categoryService;

    @GetMapping("/all")
    public Result<?> getAllCategories() {
        return Result.success("", categoryService.list());
    }

    @PostMapping("/save")
    public Result<?> saveOrUpdateCategory(@RequestBody Category category) {
        if (categoryService.saveOrUpdateCategory(category)) {
            return Result.success("操作成功", null);
        } else {
            return Result.failed(401, "操作失败");
        }
    }

    @PostMapping("/delete")
    public Result<?> deleteCategory(Long categoryId) {
        if (categoryService.removeById(categoryId)) {
            return Result.success("删除成功", null);
        } else {
            return Result.failed(401, "删除失败");
        }
    }
}
